package com.bite.springmvc.controller;

import com.bite.springmvc.model.UserInfo;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/request")
//@RestController
@Controller
@ResponseBody
public class RequestController {

    @RequestMapping("/param1")
    public String param1(String userName){
        System.out.println("接收到参数: "+userName);
        return "接收到参数: "+ userName;
    }

    @RequestMapping("/param2")
    public String param2(Integer age){
        return "2接收到参数: "+ age;
    }

    @RequestMapping("/param3")
    public String param3(int age){
        return "3接收到参数: "+ age;
    }

    @RequestMapping("/param4")
    public String param4(String userName, String password){
        return "接收到参数userName:"+ userName +", password: "+password;
    }

    @RequestMapping("/param5")
    public String param5(UserInfo userInfo){
        System.out.println(userInfo.getUserName());

        return "接收到参数userInfo:"+ userInfo;
    }


    @RequestMapping("/param6")
    public String param6(@RequestParam(value = "userName", required = false) String name, String password){
        return "接收到参数name:"+ name +", password: "+password;
    }

    @RequestMapping("/param7")
    public String param7(String[] arrParams){
        return "接收到参数arrParams:"+ Arrays.toString(arrParams);
    }

    @RequestMapping("/param8")
    public String param8(@RequestParam("listParam") List<String> listParam){
        return "接收到参数listParam:"+ listParam;
    }


    @RequestMapping("/param9")
    public String param9(@RequestBody UserInfo userInfo){
        return "接收到参数userInfo:"+ userInfo;
    }


    @RequestMapping("/param10/{articleId}")
    public String param10(@PathVariable("articleId") Integer articleId){
        return "接收到参数articleId:"+ articleId;
    }

    @RequestMapping("/param10/{id}/{name}")
    public String param11(@PathVariable("id") Integer id, @PathVariable("name") String name){
        return "接收到参数id:"+ id + ",name:"+name;
    }


    @RequestMapping("/param12")
    public String param12(@RequestPart("file1") MultipartFile file) throws IOException {
//        System.out.println(file.getName());
//        System.out.println(file.getContentType());
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("D:/temp/"+file.getOriginalFilename()));
        return "接收到文件"+file.getOriginalFilename();
    }

    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request){
//        String userName = request.getParameter("userName");
        Cookie[] cookies = request.getCookies();
        System.out.println("获取Cookie信息:");
        if (cookies!=null){
            for (Cookie cookie: cookies){
                System.out.println(cookie.getName()+ ":"+ cookie.getValue());
            }
        }
        return "获取Cookie成功";

    }

    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue("bite") String bite){
        System.out.println("从Cookkie中获取 bite:"+ bite);
        return "获取Cookie成功";
    }

    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        //参数为true  如果session对象不存在, 则创建一个session, 如果存在, 则直接返回
        HttpSession session = request.getSession(true);
        session.setAttribute("name", "zhangsan");
        session.setAttribute("studentId", "100256");
        return "设置Session成功";
    }

    //获取Session
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        //参数为false  不存在session, 返回null
        HttpSession session = request.getSession(false);
        if (session!=null){
            System.out.println(session.getAttribute("name"));
            System.out.println(session.getAttribute("studentId"));
        }
        return "获取Session成功";
    }

    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session){
        // session =  request.getSession()
        System.out.println("name: "+session.getAttribute("name"));
        System.out.println("studentId: "+session.getAttribute("studentId"));
        return "获取Session成功";
    }

    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute(value = "name", required = false) String name){
        System.out.println("name: "+ name);
        return "获取Session成功";
    }

    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        return "userAgent:" + userAgent;
    }

    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent){

        return "userAgent:" + userAgent;
    }


}
